package org.mx.test.multi.service;

import org.mx.StringUtils;
import org.mx.dal.EntityFactory;
import org.mx.dal.service.GeneralDictAccessor;
import org.mx.error.UserInterfaceSystemErrorException;
import org.mx.error.UserInterfaceSystemErrorException.SystemErrors;
import org.mx.test.multi.bean.AccountBean;
import org.mx.test.multi.bean.CodeNameTuple;
import org.mx.test.multi.db.entity.DbUserEntity;
import org.mx.test.multi.test1.entity.Test1AccountEntity;
import org.mx.test.multi.test1.entity.Test1RoleEntity;
import org.mx.test.multi.test2.entity.Test2BookEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("javadoc")
@Component
public class MultiTableOperateService {
	private GeneralDictAccessor accessorDb, accessorTest1, accessorTest2;

	@Autowired
	public MultiTableOperateService(@Qualifier("generalDictAccessor") GeneralDictAccessor accessorDb,
			@Qualifier("generalDictAccessorTest1") GeneralDictAccessor accessorTest1,
			@Qualifier("generalDictAccessorTest2") GeneralDictAccessor accessorTest2) {
		super();
		this.accessorDb = accessorDb;
		this.accessorTest1 = accessorTest1;
		this.accessorTest2 = accessorTest2;
	}

	@Transactional(transactionManager = "transactionManagerTest1", readOnly = false)
	public void saveAccount(AccountBean account) {
		if (account == null || account.getAccount() == null || StringUtils.isBlank(account.getAccount().getCode())) {
			throw new UserInterfaceSystemErrorException(SystemErrors.SYSTEM_ILLEGAL_PARAM);
		}
		String accountCode = account.getAccount().getCode();
		Test1AccountEntity accountEntity = accessorTest1.getByCode(accountCode, Test1AccountEntity.class);
		if (accountEntity == null) {
			accountEntity = EntityFactory.createEntity(Test1AccountEntity.class);
			accountEntity.setCode(accountCode);
		}
		accountEntity.setName(account.getAccount().getName());
		// 先清除原来的角色，然后加入新角色
		accountEntity.getRoles().clear();
		if (!account.getRoles().isEmpty()) {
			for (CodeNameTuple role : account.getRoles()) {
				String roleCode = role.getCode();
				Test1RoleEntity roleEntity = accessorTest1.getByCode(roleCode, Test1RoleEntity.class);
				if (roleEntity == null) {
					roleEntity = EntityFactory.createEntity(Test1RoleEntity.class);
					roleEntity.setCode(role.getCode());
				}
				roleEntity.setName(role.getName());
				roleEntity = accessorTest1.save(roleEntity);
				accountEntity.getRoles().add(roleEntity);
			}
		}
		accessorTest1.save(accountEntity);
	}

	@Transactional(readOnly = false)
	public void saveBook() {
		//
	}

	@Transactional(readOnly = false)
	public void clear() {
		clearDb();
		clearTest1();
		clearTest2();
	}

	@Transactional(transactionManager = "transactionManager", readOnly = false)
	public void clearDb() {
		accessorDb.clear(DbUserEntity.class);
	}

	@Transactional(transactionManager = "transactionManagerTest1", readOnly = false)
	public void clearTest1() {
		accessorTest1.clear(Test1AccountEntity.class);
		accessorTest1.clear(Test1RoleEntity.class);
	}

	@Transactional(transactionManager = "transactionManagerTest2", readOnly = false)
	public void clearTest2() {
		accessorTest2.clear(Test2BookEntity.class);
	}
}
